
/**
 * @param {Object} key
 * @param {Object} val
 * @param {Object} expired
 */
const set = function(key, val, expired) {
	let dte = new Date();
	let data = {
		data: val,
		expired: dte.getTime() / 1000 + (expired ? expired : 86400 * 365)
	}

	uni.setStorageSync(key, data);
}

/**
 * @param {Object} key
 * @param isDel
 */
const get = function(key, isDel = false) {

	let data = uni.getStorageSync(key);

	if (data) {
		let dte = new Date();
		if (data.expired > dte.getTime() / 1000) {
			if (isDel) {
				uni.removeStorageSync(key);
			}
			return data.data;
		} else {
			uni.removeStorageSync(key);
		}
	}
	return null;
}

const delExpired = function() {
	uni.getStorageInfo({
		success: function(res) {
			for (let i = 0; i < res.keys.length; i++) {
				get(res.keys[i]);
			}
		}
	});
}

const del = function(key) {
	uni.removeStorage({
		key: key
	})
}




export default {
	set,
	get,
	del,
	delExpired
}
